# coding=utf-8
from django.shortcuts import render
from django.http.response import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponse
import MySQLdb as mdb
import time

con = None
mysql_qa = '10.9.128.47'


# Create your views here.

def checkCount():
    con = mdb.connect(mysql_qa, 'root', 'qatest', 'qa')
    with con:
        cur = con.cursor()
        cur.execute("select num from jsp where id = 1")
        return cur.fetchall()


def insertCount(num):
    con = mdb.connect(mysql_qa, 'root', 'qatest', 'qa')
    with con:
        cur = con.cursor()
        cur.execute("update jsp SET num = '%s' where id = 1" % num)


def wedding(req):
    print req.META['HTTP_USER_AGENT']
    if 'Mobile' not in req.META['HTTP_USER_AGENT']:
        response = HttpResponse()
        response.write(
            "<script>alert('为提升观看体验,请使用手机观看,PC只能观看视频哟~'); "
            "window.location.href='http://down.23vcr.com/lmki2aFq_fVl7Nt1JPAZoCYTN-wq'</script>")
        return response
    num = checkCount()
    insert_num = int(num[0][0]) + 1
    insertCount(insert_num)
    now = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    content = {
        'num': insert_num,
        'now': str(now)
    }
    return render_to_response('jiaoshupeng/index.html', content, context_instance=RequestContext(req))
